Fix PHPStan level 0 issues in themes#10951
Fix PHPStan level 0 issues in themes#10951westonruter wants to merge 7 commits intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
@sabernhardt Would you review when you get a chance? |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| * | ||
| * @param WP_Query $query WP_Query object. | ||
| * @return WP_Query Possibly-modified WP_Query. | ||
| * @return void |
There was a problem hiding this comment.
This seems right. For history, this was already in r25808, when the Featured_Content class was added.
| * @param int $post_id The ID of the post. | ||
| * @param string $location The location where the meta is shown. | ||
| * @return string Post meta HTML. | ||
| * @return string|void Post meta HTML. |
There was a problem hiding this comment.
This function can return void in two situations, but I think returning an empty string could be better when there is no HTML to return. Maybe it's unwise to change that now, though.
There was a problem hiding this comment.
Yeah, for this it seems best to document the existing behavior.
| $repl = sprintf( '<svg class="svg-icon" width="%d" height="%d" aria-hidden="true" role="img" focusable="false" ', $size, $size ); | ||
|
|
||
| $svg = preg_replace( '/^<svg /', $repl, trim( $arr[ $icon ] ) ); // Add extra attributes to SVG code. | ||
| $svg = (string) preg_replace( '/^<svg /', $repl, trim( $arr[ $icon ] ) ); // Add extra attributes to SVG code. |
There was a problem hiding this comment.
The pattern and $repl are both strings. If $arr[ $icon ] is somehow not a string, then it will have errors with the trim() function.
| $svg = (string) preg_replace( '/^<svg /', $repl, trim( $arr[ $icon ] ) ); // Add extra attributes to SVG code. | |
| $svg = preg_replace( '/^<svg /', $repl, trim( (string) $arr[ $icon ] ) ); // Add extra attributes to SVG code. |
There was a problem hiding this comment.
The issue that PHPStan is warning about is that preg_replace() can return false. So then we'd need to add two casts. However, what about cfb02ce? This ensures the proper type up front.
There was a problem hiding this comment.
Yes, I prefer checking is_string( $arr[ $icon ] ) instead of casting it.
There was a problem hiding this comment.
if ( isset( $arr[ $icon ] ) && is_string( $arr[ $icon ] ) ) { approach make sense here
| * | ||
| * @phpstan-return ( | ||
| * $header is 'Tags' | ||
| * ? string[]|false | ||
| * : ( $header is 'Name'|'ThemeURI'|'Description'|'Author'|'AuthorURI'|'Version'|'Template'|'Status'|'TextDomain'|'DomainPath'|'RequiresWP'|'RequiresPHP'|'UpdateURI' | ||
| * ? string|false | ||
| * : false ) | ||
| * ) |
There was a problem hiding this comment.
Does class-wp-theme.php belong in this PR?
There was a problem hiding this comment.
Yes, because it fixes issues with calls to wp_get_theme()->get( 'Version' ).
There was a problem hiding this comment.
For example:
PHPStan is flagging this:
Parameter #4 $ver of function wp_enqueue_style expects bool|string|null, array|string|false given.
With the inclusion of the @phpstan-return, the issue is resolved.
|
@sabernhardt anything else you'd like revised? |
|
I have nothing further. |
… comments. Developed in #10951 Props westonruter, sabernhardt, justlevine. See #64238, #61175. git-svn-id: https://develop.svn.wordpress.org/trunk@61676 602fd350-edb4-49c9-b593-d223f7449a82
… comments. Developed in WordPress/wordpress-develop#10951 Props westonruter, sabernhardt, justlevine. See #64238, #61175. Built from https://develop.svn.wordpress.org/trunk@61676 git-svn-id: http://core.svn.wordpress.org/trunk@60984 1a063a9b-81f0-0310-95a4-ce76da25c4cd
The commits in this PR were cherry-picked from #10419 for Core-61175.
Trac ticket: https://core.trac.wordpress.org/ticket/64238
Use of AI Tools
n/a
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.